package com.k.kppcloud.gateway.biz.login;

import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("/login")
public class LoginController {
    private org.slf4j.Logger log = LoggerFactory.getLogger(LoginController.class);

    @GetMapping("/page/toPage1")
    public String login() {
        log.info("req -> /page/login ");
        return "login";
    }
    @GetMapping("/page/grid")
    public String grid() {
        return "sgrid";
    }

    @GetMapping("/page/toPage2")
    public String login2(final Model model) {
        log.info("req -> /page/login2 ");
        model.addAttribute("myStr", "");
        return "login";
    }

    @GetMapping("/page/toPage3")
    public Mono<String> login3(final Model model) {
        log.info("req -> /page/login3 ");
        model.addAttribute("name", "泥瓦匠");
        model.addAttribute("city", "浙江温岭");
        return Mono.create(monoSink -> monoSink.success("login"));
    }

    @GetMapping("/query/a1")
    @ResponseBody
    public Mono<String> hello2() {
        return Mono.justOrEmpty("hehe");
    }

    @GetMapping("/query/a2")
    public Mono<ServerResponse> hello() {
        return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON)
                .body(BodyInserters.fromValue("Hello,Spring webflux"));
    }
    @GetMapping("/query/a3")
    public Mono<ServerResponse> a3() {
        Flux<String> f = Flux.just("a","b","c");
        return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(f,String.class);
    }
}
